package com.yt.lxh.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.lxh.acl.mapper.MenuMapper;
import com.yt.lxh.acl.service.MenuService;
import com.yt.lxh.common.exception.LuXueHeException;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.common.service.CheckLogin;
import com.yt.lxh.common.utils.AuthContextUtil;
import com.yt.lxh.common.utils.TreeHelper;
import com.yt.lxh.model.acl.Admin;
import com.yt.lxh.model.acl.Menu;
import com.yt.lxh.vo.menu.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private CheckLogin checkLogin;

    //菜单列表
    @Override
    public List<Menu> findNodes() {
        //1 查询所有菜单，返回list集合
        List<Menu> menuList = menuMapper.selectList(null);
        if (CollectionUtils.isEmpty(menuList)) {
            return null;
        }
        //2 调用工具类的方法，把返回list集合封装要求数据格式
        List<Menu> treeList = TreeHelper.buildTree(menuList);
        return treeList;
    }

    //菜单删除
    @Override
    public void deleteById(Long id) {
        //根据当前菜单id，查询是否包含子菜单
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getParentId,id);
        Integer count = menuMapper.selectCount(wrapper);
        //判断，count大于0，包含子菜单
        if (count>0){
            throw new LuXueHeException(ResultCodeEnum.NODE_ERROR);
        }
        //count等于0，直接删除
        menuMapper.deleteById(id);
    }

    //添加菜单
    @Override
    public void saveMenu(Menu menu,String token) {
        //获取创建者以及修改者
        Admin admin = checkLogin.checkAdminLogin();
        menu.setCreateUser(admin.getUsername());
        menu.setUpdateUser(admin.getUsername());
        menuMapper.insert(menu);
    }

    //菜单修改
    @Override
    public void updateMenuById(Menu menu,String token) {
        //获取修改者
        Admin admin = checkLogin.checkAdminLogin();
        //设置修改者
        menu.setUpdateUser(admin.getUsername());
        //设置修改时间
        Date now = new Date();
        menu.setUpdateTime(now);
        menuMapper.updateById(menu);
    }

    //查询用户可以操作菜单
    @Override
    public List<MenuVo> findMenusByUserId() {
        //获取当前用户id
        Admin admin = AuthContextUtil.get();
        Long adminId = admin.getId();
        //根据userId查询可以操作菜单
        List<Menu> menuListByUserId = menuMapper.findMenusByUserId(adminId);
        //封装要求数据格式，返回
        List<Menu> menuList = TreeHelper.buildTree(menuListByUserId);
        List<MenuVo> menuVoList = this.buildMenus(menuList);
        return menuVoList;
    }

    // 将List<MenuVo>对象转换成List<MenuVo>对象
    private List<MenuVo> buildMenus(List<Menu> menus) {

        List<MenuVo> menuVoList = new LinkedList<MenuVo>();
        for (Menu menu: menus) {
            MenuVo menuVo = new MenuVo();
            menuVo.setTitle(menu.getTitle());
            menuVo.setName(menu.getComponent());
            List<Menu> children = menu.getChildren();
            if (!CollectionUtils.isEmpty(children)) {
                menuVo.setChildren(buildMenus(children));
            }
            menuVoList.add(menuVo);
        }
        return menuVoList;
    }
}
